/*
 * Copyright 2013 floreysoft GmbH (www.floreysoft.net)
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.floreysoft.gwt.drive.client.realtime;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;

/**
 * A collaborative map. A map's key must be a string. The values can contain
 * other Realtime collaborative objects, custom collaborative objects,
 * JavaScript primitive values or JavaScript objects that can be serialized to
 * JSON.
 * 
 * Changes to the map will automatically be synced with the server and other
 * collaborators. To listen for changes, add EventListeners for the
 * gapi.drive.realtime.EventType.VALUE_CHANGED event type.
 * 
 * This class should not be instantiated directly. To create a new map, use
 * gapi.drive.realtime.Model.prototype.createMap().
 * 
 * @param <T>
 */
public final class CollaborativeMap<T> extends CollaborativeObject {

	protected CollaborativeMap() {

	}

	/**
	 * Put the value into the map with the given key, overwriting an existing
	 * value for that key.
	 * 
	 * @param key
	 *            The map key.
	 * @param value
	 *            The map value.
	 * @return The old map value, if any, that used to be mapped to the given
	 *         key.
	 */
	public native T set(String key, T value)/*-{
		return this.set(key, value);
	}-*/;

	/**
	 * Returns the value mapped to the given key.
	 * 
	 * @param key
	 *            The key to look up.
	 * @return The value mapped to the given key.
	 */
	public native T get(String key)/*-{
		return (this.get(key) == null) ? null : this.get(key);
	}-*/;

	/**
	 * Removes the entry for the given key (if such an entry exists).
	 * 
	 * @param key
	 *            The key to unmap.
	 * @return The value that was mapped to this key, or null if there was no
	 *         existing value.
	 */
	public native T delete(String key)/*-{
		this['delete'](key);
	}-*/;

	/**
	 * Removes all entries.
	 */
	public native void clear()/*-{
		this.clear();
	}-*/;

	/**
	 * Returns an array containing a copy of the keys in this map. Modifications
	 * to the returned array do not modify this collaborative map.
	 * 
	 * @return
	 */
	public native JsArrayString keys() /*-{
		return this.keys();
	}-*/;

	/**
	 * Checks if this map contains an entry for the given key.
	 * 
	 * @param key
	 *            The key to check.
	 * @return Returns true if this map contains a mapping for the given key.
	 */
	public native boolean has(String key) /*-{
		return !!this.has(key);
	}-*/;

	/**
	 * Returns whether this map is empty.
	 * 
	 * @return Returns true if this map is empty.
	 */
	public native boolean isEmpty() /*-{
		return !!this.isEmpty();
	}-*/;

	/**
	 * Returns an array containing a copy of the values in this map.
	 * Modifications to the returned array do not modify this collaborative map.
	 * 
	 * @return The values in this map.
	 */
	@SuppressWarnings("rawtypes")
	public native JsArray values() /*-{
		return this.values();
	}-*/;

	/**
	 * @return The number of keys in the map.
	 */
	public native int size() /*-{
		return this.size;
	}-*/;

	/**
	 * Returns an array containing a copy of the items in this map.
	 * Modifications to the returned array do not modify this collaborative map.
	 * 
	 * @return The items in this map. Each item is a [key, value] pair.
	 */
	@SuppressWarnings("rawtypes")
	public native JsArray items() /*-{
		return this.items();
	}-*/;

}
